package com.qzsoft.zhjd.server.common.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 功能描述:Map转化为Bean
 *
 * @author : liugf
 * @date : 2020/6/9 15:01
 */
public class MapToBeanUtils {

    private static final char UNDERLINE = '_';

    /**
     * 功能描述:把Map转化指定的Bean
     *
     * @param map       参数
     * @param beanClass javaBean
     * @return : T
     * @author : liugf
     * @date : 2020/6/9 15:05
     */
    public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) throws Exception {
        if (map == null)
            return null;
        T obj = beanClass.newInstance();
        ConvertUtils.register(new DataCollectDataConverter(), Date.class);
        BeanUtils.populate(obj, map);
        return obj;
    }

    public static <T> T mapToCapitalObject(Map<String, Object> map, Class<T> beanClass) throws Exception {
        if (map == null)
            return null;
        T obj = beanClass.newInstance();
        Field[] declaredFields = beanClass.getDeclaredFields();
        for (Field field : declaredFields) {

            field.setAccessible(true);
            if (field.getType().getName().equals("java.math.BigDecimal")) {
                if (ObjectUtils.isNotEmpty( map.get(field.getName().toUpperCase()))) {
                    field.set(obj, new BigDecimal(String.valueOf(map.get(field.getName().toUpperCase()))));
                }
            } else if (field.getType().getName().equals("java.time.LocalDateTime")) {
                if (ObjectUtils.isNotEmpty(map.get(field.getName().toUpperCase()))) {
                    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    field.set(obj, LocalDateTime.parse(map.get(field.getName().toUpperCase()).toString(),fmt));
                }

            }else if(field.getType().getName().equals("java.util.Date")) {
                if (ObjectUtils.isNotEmpty(map.get(field.getName().toUpperCase()))) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    field.set(obj,simpleDateFormat.parse(map.get(field.getName().toUpperCase()).toString()));
                }

            }else {
                if (ObjectUtils.isNotEmpty(map.get(field.getName().toUpperCase()))) {
                    field.set(obj, map.get(field.getName().toUpperCase()));
                }
            }

        }
        return obj;
    }


    /**
     * 功能描述: 把map的key转换成驼峰命名
     *
     * @param map map
     * @return : java.util.Map
     * @author : liugf
     * @date : 2020/6/9 15:02
     */
    public static Map<String, Object> toReplaceKeyLow(Map<String, Object> map) {
        Map re_map = new HashMap();
        if (re_map != null) {
            Iterator var2 = map.entrySet().iterator();

            while (var2.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry) var2.next();
                re_map.put(underlineToCamel((String) entry.getKey()), map.get(entry.getKey()));
            }
            map.clear();
        }
        return re_map;
    }

    /**
     * 功能描述: 把下划线变成驼峰名
     *
     * @param param key
     * @return : java.lang.String
     * @author : liugf
     * @date : 2020/6/9 15:39
     */
    private static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(Character.toLowerCase(param.charAt(i)));
            }
        }
        return sb.toString();
    }

}
